﻿using System;
using System.Collections.Specialized;
using System.IO;
using System.Windows.Forms;

namespace GDFlacTool.Forms
{
    public partial class FormSelectImageEditor : Form
    {
        private readonly StringCollection _editors;
        private string _currentEditor = string.Empty;

        public FormSelectImageEditor(StringCollection editors, string currentEditor)
        {
            InitializeComponent();

            _editors = editors;
            _currentEditor = currentEditor;
            
            if(Editors == null)
            {
                btnOK.Enabled = false;
            }
            else
            {
                LoadEditors();
                btnOK.Enabled = true;
            }
        }

        public string CurrentEditor
        {
            get { return _currentEditor; }
        }

        public StringCollection Editors
        {
            get { return _editors; }
        }

        private void LoadEditors()
        {
            int count = 0;

            clbEditors.Items.Clear();

            foreach (var editor in Editors)
            {
                var itm = new CheckedListBoxItem {Value = editor, Text = Path.GetFileNameWithoutExtension(editor)};

                clbEditors.Items.Add(itm);

                if (itm.Text == Path.GetFileNameWithoutExtension(CurrentEditor))
                {
                    clbEditors.SetItemCheckState(count, CheckState.Checked);
                }

                count++;
            }
        }

        private void btnAddEditor_Click(object sender, EventArgs e)
        {
            openFileDialog.Title = "Add Image Editor";
            openFileDialog.Filter = "Executable file(*.exe)|*.exe";
            openFileDialog.FilterIndex = 1;
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

            DialogResult open = openFileDialog.ShowDialog(this);

            if (open == DialogResult.OK)
            {
                if(!Editors.Contains(openFileDialog.FileName))
                {
                    Editors.Add(openFileDialog.FileName);

                    LoadEditors();  
                }

                if(clbEditors.Items.Count == 1)
                {
                    clbEditors.SetItemCheckState(0, CheckState.Checked);
                }
            }
        }

        private void btnRemoveImageEditor_Click(object sender, EventArgs e)
        {
            if(clbEditors.CheckedItems.Count == 1)
            {
                string remove = ((CheckedListBoxItem) clbEditors.CheckedItems[0]).Value;

                clbEditors.Items.Remove(clbEditors.CheckedItems[0]);

                Editors.Remove(remove);

                LoadEditors();
            }
        }

        private void clbEditors_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (clbEditors.CheckedItems.Count == 1)
            {
                Boolean isCheckedItemBeingUnchecked = (e.CurrentValue == CheckState.Checked);
                if (isCheckedItemBeingUnchecked)
                {
                    e.NewValue = CheckState.Checked;
                }
                else
                {
                    Int32 checkedItemIndex = clbEditors.CheckedIndices[0];
                    clbEditors.ItemCheck -= clbEditors_ItemCheck;
                    clbEditors.SetItemChecked(checkedItemIndex, false);
                    clbEditors.ItemCheck += clbEditors_ItemCheck;
                }
            } 
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            _currentEditor = clbEditors.CheckedItems.Count == 1 ? ((CheckedListBoxItem) clbEditors.CheckedItems[0]).Value : string.Empty;
        }
    }

    internal class CheckedListBoxItem
    {
        private string _value = string.Empty;
        private string _text = string.Empty;

        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        public override string ToString()
        {
            return _text;
        }
    }
}
